/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.DataTypeInterfaceGeneratorContext;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefModelResInfoImpl;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;

public class InitChildInfosGenerator extends ClassMethodGenerator {

  private final DataTypeInterfaceGeneratorContext parentContext;
  private final IGspCommonObject dataType;

  public InitChildInfosGenerator(IGspCommonObject dataType,
      DataTypeInterfaceGeneratorContext parentContext) {
    this.dataType = dataType;
    this.parentContext = parentContext;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    for (IGspCommonObject child : dataType.getContainChildObjects()) {
      MediateType entityType = parentContext.getApiGenContext().getCefGenContext()
          .getEntityResourceInfos().get(child.getCode());
      String typeName = entityType.getFullName();
      TypeInfo type = new TypeInfo(typeName);
      type.setUseFullName(true);

      MethodInvocation invocation = ast.newMethodInvocation();
      invocation.setName(ast.newSimpleName("addChildEntityResInfo"));
      ClassInstanceCreation creation = ast.newClassInstanceCreation();
      creation.setType(type.getType(ast, parentContext.getApiGenContext()));
      creation.arguments().add(ast.newSimpleName(parName_ModelInfo));
      invocation.arguments().add(creation);
      block.statements().add(ast.newExpressionStatement(invocation));
    }
    return block;
  }

  @Override
  protected String getMethodName() {
    return "initChildInfos";
  }

  @Override
  protected TypeInfo getReturnType() {
    return null;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
    return list;
  }

  private static final String parName_ModelInfo = "modelInfo";

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return new ArrayList<ParameterInfo>() {{
      add(new ParameterInfo() {{
        setParamName(parName_ModelInfo);
        setParamType(new TypeInfo(
            CefModelResInfoImpl.class));
      }});
    }};
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }
}
